<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<script>
		$(function () {
			getData();
			$("#addSpec").select2();
			$("#addBrand").select2();
			$("#updateSpec").select2();
			$("#updateBrand").select2();
		})
		function getData(obj) {
			var sname = $("#sname").val();
			$.ajax({
				url:"/templates/getinfo",
				type:"post",
				data:{sname:sname,number:obj},
				dataType:"json",
				success:function (result) {
					var list = result.data.result;
					var b = result.data;
					var content = "";
					for (var i=0;i<list.length;i++){
						var specIds = list[i].specIds;
						var specIdsJson = JSON.parse(specIds);
						console.log(specIdsJson);
						var specName = "";
						for (var j =0;j<specIdsJson.length;j++){
							specName += "," +specIdsJson[j].text;
						}
						specName = specName.substring(1);

						var brandIds = list[i].brandIds;
						var brandIdsJson = JSON.parse(brandIds);
						var brandName = "";
						for (var j =0;j<brandIdsJson.length;j++){
							brandName += "," +brandIdsJson[j].text;
						}
						brandName = brandName.substring(1);

						var items = list[i].customAttributeItems;
						var itemJson = JSON.parse(items);
						var item = "";
						for (var j =0;j<itemJson.length;j++){
							item += "," +itemJson[j].text;
						}
						item = item.substring(1);

						content += '<tr>\n' +
								'<td><input  type="checkbox" value="'+list[i].id+'"></td>\t\t'+
								'\t\t\t\t                          <td>'+list[i].id+'</td>\n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+list[i].name+'</td>\n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+specName+'</td>\n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+brandName+'</td>\t\t\t\t\t\t\t\t\t \n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+item+'</td>\t\t                                                                  \n' +
								'\t\t                                  <td class="text-center">                                           \n' +
								'\t\t                                 \t  <button type="button" onclick="toUpdate('+list[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>                                           \n' +
								'\t\t                                  </td>\n' +
								'\t\t\t                          </tr>'
					}
					$("#typeTable").html(content);

					//分页
					var pageinfo = "";
					pageinfo += '<li><a href="javascript:getData(1)">首页</a></li>'
					if (b.pageNum > 1) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum - 1) + ')">上一页</a></li>'
					}
					pageinfo += '<li class="threeword">第 ' + b.pageNum + ' 页</li>'
					if (b.pageNum < b.pages) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum + 1) + ')">下一页</a></li>'
					}
					pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pages) + ')">末页</a></li>'
					$("#page").html(pageinfo);
				},
				error:function (){
					alert("查询失败");
				}
			})
		}

		function toAdd() {
			$.ajax({
				url:"/templates/toAdd",
				type:"post",
				dataType:"json",
				success:function (result) {
					var slist = result.slist;
					var blist = result.blist;
					var bcount = "";
					var scount = "";
					for (var i = 0;i<slist.length;i++){
						scount += '<option value="'+slist[i].id+'">'+slist[i].specName+'</option>'
					}
					for (var i = 0;i<blist.length;i++){
						bcount += '<option value="'+blist[i].id+'">'+blist[i].name+'</option>'
					}
					$("#addBrand").html(bcount);
					$("#addSpec").html(scount);
				},
				error:function (){
					alert("增加失败");
				}
			})
		}

		function addinfo() {
			var brandOptions = $("#addBrand").select2('data');
			var brandList = [];
			$(brandOptions).each(function (i,e) {
				brandList.push({"id":e.id,"text":e.text});
			})
			var brands = JSON.stringify(brandList);

			var specOptions = $("#addSpec").select2('data');
			var specList = [];
			$(specOptions).each(function (i,e) {
				specList.push({"id":e.id,"text":e.text});
			})
			var specs = JSON.stringify(specList);

			var options = [];
			var table = $("#temTable");
			var trs = $(table).find("tr");
			for (var i = 1;i < trs.length;i++){
				var tds = $(trs[i]).find("td");
				var option = {"text":$(tds[1]).find("input").val()};
				options.push(option);
			}
			var items = JSON.stringify(options);
			$.ajax({
				url:"/templates/addinfo",
				type:"post",
				data:$("#addForm").serialize()+"&brands="+brands+"&specs="+specs+"&items="+items,
				dataType:"json",
				async:false,
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function (){
					alert("增加失败");
				}
			})
		}
		function deleteAll(){
			var ids = [];
			var rows = $("input:checked");
			$(rows).each(function (i,e) {
				ids.push(e.value);
			})
			if (ids.length<=0){
				alert("请选择需要删除的内容");
				return ;
			}
			$.ajax({
				url:"/templates/deleteAll",
				type:"post",
				data:{ids:ids},
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}

		function toUpdate(obj) {
			$.ajax({
				url:"/templates/toUpdate",
				type:"post",
				data:{id:obj},
				dataType:"json",
				async:false,
				success:function (result) {
					var slist = result.data.slist;
					var blist = result.data.blist;
					var bcount = "";
					var scount = "";
					for (var i = 0;i<slist.length;i++){
						scount += '<option value="'+slist[i].id+'">'+slist[i].specName+'</option>'
					}
					for (var i = 0;i<blist.length;i++){
						bcount += '<option value="'+blist[i].id+'">'+blist[i].name+'</option>'
					}
					$("#updateBrand").html(bcount);
					$("#updateSpec").html(scount);
					var brands = result.data.tem.brandIds;
					var brand = JSON.parse(brands);
					var ids = [];
					for (var i =0;i<brand.length;i++){
						ids.push(brand[i].id);
					}
					$("#updateBrand").val(ids).trigger("change");

					var specs = result.data.tem.specIds;
					var spec = JSON.parse(specs);
					var cds = [];
					for (var i=0;i<spec.length;i++){
						cds.push(spec[i].id);
					}
					$("#updateSpec").val(cds).trigger("change");
					var updateId = result.data.tem.id;
					var name = result.data.tem.name;
					$("#updateId").val(updateId);
					$("#udpateName").val(name);
					var content = ' <thead>\n' +
							'\t\t\t\t\t\t\t\t\t\t  <tr>\n' +
							'\t\t\t\t\t\t\t\t\t\t\t  <td><input type="checkbox" class="icheckbox_square-blue"></td>\n' +
							'\t\t\t\t\t\t\t\t\t\t\t  <td>属性名称</td>\n' +
							'\t\t\t\t\t\t\t\t\t\t\t  <td>操作</td>\n' +
							'\t\t\t\t\t\t\t\t\t\t  </tr>\n' +
							'\t\t\t\t\t\t\t\t\t\t  </thead>\n' +
							'\t\t\t\t\t\t\t\t\t\t  <tbody id="updateTab">';
					var item = result.data.tem.customAttributeItems;
					var items = JSON.parse(item);

					console.log(items);
					for (var i = 0; i < items.length; i++) {
						content += '<tr>\n' +
								'\t\t\t\t\t\t\t\t\t\t\t  <td><input type="checkbox" class="icheckbox_square-blue" ></td>\n' +
								'\t\t\t\t\t\t\t\t\t\t\t  <td><input class="form-control" placeholder="属性名称" value="'+items[i].text+'"></td>\n' +
								'\t\t\t\t\t\t\t\t\t\t\t  <td><button type="button" class="btn btn-default" onclick="dalete(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>\n' +
								'\t\t\t\t\t\t\t\t\t\t  </tr>';
					}
					content += '</tbody>';
					$("#updateTable").html(content);
				},
				error:function (){
					msg("修改失败");
				}
			})
		}
		function updateinfo() {
			var brandOptions = $("#updateBrand").select2('data');
			var brandList = [];
			$(brandOptions).each(function (i,e) {
				brandList.push({"id":e.id,"text":e.text});
			})
			var brandIds = JSON.stringify(brandList);

			var specOptions = $("#updateSpec").select2('data');
			var specList = [];
			$(specOptions).each(function (i,e) {
				specList.push({"id":e.id,"text":e.text});
			})
			var specIds = JSON.stringify(specList);

			var options = [];
			var table = $("#updateTable");
			var trs = $(table).find("tr");
			for (var i = 1;i < trs.length;i++){
				var tds = $(trs[i]).find("td");
				var option = {"text":$(tds[1]).find("input").val()};
				options.push(option);
			}
			var customAttributeItems = JSON.stringify(options);

			var id = $("#updateId").val();
			var name = $("#udpateName").val();
			$.ajax({
				url:"/templates/updateinfo",
				type:"post",
				data:{id:id,name:name,brandIds:brandIds,specIds:specIds,customAttributeItems:customAttributeItems},
				dataType:"json",
				async:false,
				success:function (result) {
					getSearch();
					alert(result.message);
				},
				error:function (){
					alert("修改失败");
				}
			})
		}
		function getSearch() {
			location.href="/templates/toTypeTemplates"
		}
	</script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" onclick="toAdd()"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  type ="text" id="sname">
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
										  <th class="" style="padding-right:0px">
											  <input id="selall" type="checkbox" class="icheckbox_square-blue">
										  </th>
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="typeTable">
			                        <!--  <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
							<div class="pageNum" id="page">
							</div>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 -->
  <form id="addForm">
	  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="addModalLabel">商品类型模板编辑</h3>
				  </div>
				  <div class="modal-body">

					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>商品类型</td>
							  <td><input  class="form-control" placeholder="商品类型" name="name">  </td>
						  </tr>
						  <tr>
							  <td>关联品牌</td>
							  <td>
									<select id="addBrand" style="width:100%" multiple>

									</select>
							  </td>
						  </tr>
						  <tr>
							  <td>关联规格</td>
							  <td>
								  <select id="addSpec" style="width:100%" multiple>

								  </select>
							  </td>
						  </tr>

						  <tr>
							  <td>扩展属性</td>
							  <td>
								  <div class="btn-group">
									  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

								  </div>
								  <table id="temTable" class="table table-bordered table-striped"  width="800px">
									  <thead  id="addTable">
									  <tr>
										  <td><input type="checkbox" class="icheckbox_square-blue"></td>
										  <td>属性名称</td>
										  <td>操作</td>
									  </tr>
									  </thead>
									  <tbody>
									  <tr>
										  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
										  <td><input class="form-control" placeholder="属性名称" ></td>
										  <td><button type="button" class="btn btn-default" onclick="dalete(this)" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
									  </tr>
									  </tbody>
								  </table>

							  </td>
						  </tr>

					  </table>

				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addinfo()">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>


  <script type="text/javascript">
	  function addmake(){
		  var tab=document.getElementById("addTable");
		  var tr=document.createElement("tr");
		  tr.innerHTML='<td><input  class="form-control" placeholder="规格选项" name="option_name"></td><td><input  class="form-control" placeholder="排序" name="orders"></td><td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
		  tab.appendChild(tr);
	  }

	  function updatemake(){
		  var tab=document.getElementById("updateTab");
		  var tr=document.createElement("tr");
		  tr.innerHTML='<td><input  class="form-control" placeholder="规格选项" name="option_name"></td><td><input  class="form-control" placeholder="排序" name="orders"></td><td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
		  tab.appendChild(tr);
	  }

	  function dalete(obj){
		  var d =(obj.parentNode.parentNode);
		  d.parentNode.removeChild(d);
	  }
  </script>

<!--//修改窗口-->
  <form id="updateForm">
		<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			  <div class="modal-dialog" >
				  <div class="modal-content">
					  <div class="modal-header">
						  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						  <h3 id="myModalLabel">商品类型模板编辑</h3>
					  </div>
					  <div class="modal-body">

						  <table class="table table-bordered table-striped"  width="800px">
							  <tr>
								  <td>商品类型</td>
								  <td><input  class="form-control" placeholder="商品类型" name="name" id="udpateName">
								  		<input type="hidden" name="id"  id="updateId">
								  </td>
							  </tr>
							  <tr>
								  <td>关联品牌</td>
								  <td>
									  <select id="updateBrand" style="width:100%" multiple>

									  </select>
								  </td>
							  </tr>
							  <tr>
								  <td>关联规格</td>
								  <td>
									  <select id="updateSpec" style="width:100%" multiple>

									  </select>
								  </td>
							  </tr>

							  <tr>
								  <td>扩展属性</td>
								  <td>
									  <div class="btn-group">
										  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

									  </div>
									  <table id="updateTable" class="table table-bordered table-striped"  width="800px">
										  <thead>
										  <tr>
											  <td><input type="checkbox" class="icheckbox_square-blue"></td>
											  <td>属性名称</td>
											  <td>操作</td>
										  </tr>
										  </thead>
										  <tbody id="updateTab">
										  <tr>
											  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
											  <td><input class="form-control" placeholder="属性名称" ></td>
											  <td><button type="button" onclick="dalete(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
										  </tr>
										  </tbody>
									  </table>

								  </td>
							  </tr>

						  </table>

					  </div>
					  <div class="modal-footer">
						  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateinfo()">保存</button>
						  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
					  </div>
				  </div>
			  </div>
		  </div>
  </form>
    
</body>

</html>